using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Windows.Forms.Design;

namespace InterfaceToPropertyGrid
{

    public class TrackBarEditor : System.Drawing.Design.UITypeEditor
    {
        public TrackBarEditor()
        {
        }

        // Indicates whether the UITypeEditor provides a form-based (modal) dialog, 
        // drop down dialog, or no UI outside of the properties window.
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        // Displays the UI for value selection.
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService edSvc;

            // Return the value if the value is not of type Int32, Double and Single.
            if (value.GetType() != typeof(int))
                return value;

            if (provider != null)
                // Uses the IWindowsFormsEditorService to display a 
                // drop-down UI in the Properties window.
                edSvc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            else
                edSvc = null;

            if (edSvc != null)
            {
                edSvc.CloseDropDown();

                // Display an angle selection control and retrieve the value.
                TrackBar trackbarControl = new TrackBar();
                trackbarControl.Minimum = 0;
                trackbarControl.Maximum = 1000;
                trackbarControl.Value = 0;
                
                edSvc.DropDownControl(trackbarControl);
                
                // Return the value in the appropraite data format.
                value = trackbarControl.Value;
                //return trackbarControl.Value;
            }
            
            return value;
        }

    }
}
